//
//  DLGuidePage.m
//  EggSteward
//
//  Created by 王道林 on 16/9/19.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "DLGuidePage.h"
#import "DLMacro.h"
BOOL isout = NO;
@interface DLGuidePage ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSArray *arrGuide;
@end


@implementation DLGuidePage



- (instancetype)initWithFrame:(CGRect)frame arr:(NSArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        // 添加引导页
            self.arrGuide = arr;
            [self makeLaunchView];
            [self creatPage];
    }
    return self;
}


- (void)makeLaunchView
{
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scroll.contentSize = CGSizeMake(SCREEN_WIDTH * _arrGuide.count, SCREEN_HEIGHT);
    scroll.pagingEnabled = YES;
    scroll.delegate = self;
    [self addSubview:scroll];
    for (int i = 0; i < _arrGuide.count; i++) {
        UIImageView *iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        iamgeView.image = [UIImage imageNamed:_arrGuide[i]];
        [scroll addSubview:iamgeView];
    }
    
}

- (void)creatPage
{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT - 50, 100, 40)];
    //当前页
    self.page.currentPage = 0;
    self.page.numberOfPages = _arrGuide.count;
    [self addSubview:self.page];
}

// 第一类: 监听滚动 让图控制点,: 把偏移量转化contenoffset为点currentPage

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > (_arrGuide.count - 1) *SCREEN_WIDTH + 30) {
        isout = YES;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 让点控制图, 把currentPage 转化contentOffset
    self.page.currentPage = index;
    
    if (isout) {
        [UIView animateWithDuration:1.5 animations:^{
            self.alpha = 0;
            scrollView.alpha = 0;
        }completion:^(BOOL finished) {
            [self removeFromSuperview];
            self.page.hidden = YES;
        }];
    }
}



@end
